﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuickDev.Common.Data
{
    /// <summary>
    /// Paged list
    /// </summary>
    /// <typeparam name="T">T</typeparam>
    public class PagedList<T> : List<T> , IPagedList
    {
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        public PagedList( IQueryable<T> source , int pageIndex , int pageSize )
        {
            int total = source.Count();
            this.TotalCount = total;
            this.TotalPages = total / pageSize;

            if ( total % pageSize > 0 )
                TotalPages++;

            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.AddRange( source.Skip( pageIndex * pageSize ).Take( pageSize ).ToList() );
        }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        public PagedList( List<T> source , int pageIndex , int pageSize )
        {
            TotalCount = source.Count();
            TotalPages = TotalCount / PageSize;

            if ( TotalCount % pageSize > 0 )
                TotalPages++;

            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.AddRange( source.Skip( pageIndex * pageSize ).Take( pageSize ).ToList() );
        }

        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public bool HasPreviousPage
        {
            get { return ( PageIndex > 0 ); }
        }
        public bool HasNextPage
        {
            get { return ( PageIndex + 1 < TotalPages ); }
        }
    }
}
